package cn.lopr.im.android.mvp.presenter;

import java.util.Map;

import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.mvp.contact.ILoginContact;
import cn.lopr.im.android.mvp.contact.IRegisterBeforeContact;
import cn.lopr.im.android.mvp.model.LoginModel;
import cn.lopr.im.android.mvp.model.RegisterBeforeModel;
import cn.lopr.im.android.utils.GsonUtil;
import cn.lopr.im.android.utils.UiUtils;
import cn.lopr.im.android.utils.cos.CosProperties;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class RegisterBeforePresenterImpl implements IRegisterBeforeContact.IRegisterBeforePresenter {
    private final IRegisterBeforeContact.IRegisterBeforeModel registerBeforeModel;
    private final IRegisterBeforeContact.IRegisterBeforeView registerBeforeView;
    private final ILoginContact.ILoginModel loginModel;
    private final Activity activity;


    public RegisterBeforePresenterImpl(Activity activity, IRegisterBeforeContact.IRegisterBeforeView registerBeforeView) {
        this.registerBeforeModel = new RegisterBeforeModel();
        this.loginModel = new LoginModel();
        this.registerBeforeView = registerBeforeView;
        this.activity = activity;
    }

    @Override
    public void getCode() {

        if (!UiUtils.isNetworkAvailable()) {
            registerBeforeView.showNetworkError();
            return;
        }

        registerBeforeView.showLoading(this.activity);

        registerBeforeModel.getCode(registerBeforeView.getPhone(), new OnCallBackListener<CallbackResult<User>, String>() {
            @Override
            public void success(CallbackResult<User> result) {
                registerBeforeView.disLoading();
                registerBeforeView.showVerifyFailed("获取验证码成功！");
            }

            @Override
            public void failure(String errMsg) {
                registerBeforeView.disLoading();
                registerBeforeView.showVerifyFailed(errMsg);
            }
        });
    }

    @Override
    public void regBefore() {

        if (!UiUtils.isNetworkAvailable()) {
            registerBeforeView.showNetworkError();
            return;
        }

        registerBeforeView.showLoading(this.activity);

        registerBeforeModel.regBefore(registerBeforeView.getPhone(), registerBeforeView.getPassword(), registerBeforeView.getSex(),
                registerBeforeView.getPortrait(), registerBeforeView.getIntroduction(),
                new OnCallBackListener<CallbackResult<Map<String, Object>>, String>() {
                    @Override
                    public void success(CallbackResult<Map<String, Object>> result) {
                        registerBeforeView.disLoading();
                        String token = (String) result.getData().get("token");
                        User user = GsonUtil.mapToObject(result.getData().get("user"), User.class);
                        registerBeforeView.regBeforeSuccess(user);
                        registerBeforeView.saveJwtKey("token", token);
                    }

                    @Override
                    public void failure(String errMsg) {
                        registerBeforeView.disLoading();
                        registerBeforeView.regBeforeFailure(errMsg);
                    }
                });
    }

    @Override
    public void getCosTempSecret(OnCallBackListener<CosProperties, String> onCallBackListener) {

        registerBeforeModel.getCosTempSecret(new OnCallBackListener<CosProperties, String>() {
            @Override
            public void success(CosProperties data) {
                onCallBackListener.success(data);
            }

            @Override
            public void failure(String errMsg) {
                registerBeforeView.regBeforeFailure(errMsg);
            }
        });
    }


}
